//数据库执行语句函数
const { sqlPromise } = require("../db/sqlPromise");
const {tree}=require('../common/common')
//获取权限列表
exports.rights = async (ctx)=>{
    let type = ctx.params.type
    let results = await sqlPromise('select a.ps_id,a.ps_name,a.ps_level,a.ps_pid,b.ps_api_path from sp_permission a join sp_permission_api b on a.ps_id=b.ps_id')
    let data = results.reduce((preVal,curVal,index)=>{
        let {ps_id:id,ps_name:authName,ps_level:level,ps_pid:pid,ps_api_path:path}=curVal
        preVal[index]={id,authName,level,pid,path:path==''?null:path}
        return preVal
    },[])
    if(type=='list'){
        ctx.body={
            data:data,
            meta:{msg:"获取权限列表成功",
            status:200}
        }
    }else if(type=='tree'){
        ctx.body={
            data:tree(data),
            meta:{msg:"获取权限列表成功",
            status:200}
        }
    }
}